//
//  TamerScene.cpp
//  beast_tamer
//
//  Created by Yang Bean on 13/5/7.
//
//

#include "TamerScene.h"
#include "CCScrollView.h"
#include "ItemNode.h"
//#include "CCTableView.h"
//#include "CCTableViewCell.h"
using namespace cocos2d;
using namespace extension;
CCScene* TamerScene::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
//    TamerScene *layer = new TamerScene();
//    layer->initWithColor(ccc4(204, 204, 204, 255));
//    layer->autorelease();
    TamerScene *layer = TamerScene::create();
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}

TamerScene::TamerScene(void)
{
    
}
TamerScene::~TamerScene(void)
{
    
}
bool TamerScene::init(void)
{
    m_currentDragSprite = NULL;
    
    
    this->setTouchEnabled(true);
    this->setTouchMode(kCCTouchesOneByOne);
    m_winSize = CCDirector::sharedDirector()->getWinSize();
    //右下角背景
    CCSprite* skillInstBGSprite = CCSprite::create("skill_inst.png");
    this->addChild(skillInstBGSprite);
    skillInstBGSprite->setAnchorPoint(ccp(1, 0));
    m_rightDownSize = skillInstBGSprite->getTextureRect().size;
    
    //右上角背景
    CCSprite* skillFormBGSprite = CCSprite::create("skill_form.png");
    this->addChild(skillFormBGSprite);
    m_rightUpSize = skillFormBGSprite->getTextureRect().size;
    skillFormBGSprite->setAnchorPoint(ccp(1, 0));
    
    
    //左上角
    CCSprite* leftTopBGSprite = CCSprite::create("top_frame.png");
    this->addChild(leftTopBGSprite);
    leftTopBGSprite->setAnchorPoint(ccp(0, 1));
    m_leftUpSize = leftTopBGSprite->getTextureRect().size;
    
    CCSprite* skillBGSprite = CCSprite::create("skillbg_tamer.png");
    skillBGSprite->setPosition(ccp(m_leftUpSize.width / 2,
                                   m_leftUpSize.height / 2));
    leftTopBGSprite->addChild(skillBGSprite);
    
    
    //左下角    
    CCSprite* leftdownBGSprite = CCSprite::create("left_frame.png");
    this->addChild(leftdownBGSprite);
    leftdownBGSprite->setAnchorPoint(ccp(0, 0));
    
    
    float hGap = (m_winSize.height - (m_leftUpSize.height +
                  leftdownBGSprite->getTextureRect().size.height)) / 2;
    float wGap = (m_winSize.width - (m_leftUpSize.width + m_rightUpSize.width)) / 2 - 5;
    
    leftTopBGSprite->setPosition(ccp(wGap,
                                     m_winSize.height - hGap));
    leftdownBGSprite->setPosition(ccp(wGap, hGap));
    skillInstBGSprite->setPosition(ccp(m_winSize.width - wGap, hGap));
    skillFormBGSprite->setPosition(ccp(m_winSize.width - wGap,
                                       hGap + m_rightDownSize.height));
    

///////////////////////////////////////////////////////////////////////////////////////////////
    m_leftDownSize = leftdownBGSprite->getTextureRect().size;
    CCScrollView* scrollView = CCScrollView::create(m_leftDownSize);
    scrollView->setBounceable(false);
    leftdownBGSprite->addChild(scrollView);
    CCArray * array = CCArray::create();
    float height = 0;
    CCSprite* sprite;
    float gap = 0;
    for (int i = 0; i < 10; i++)
    {
        if (i == 0) {
            sprite = CCSprite::create("feats_used.png");
        }
        else if(i % 3 == 1)
        {
            sprite = CCSprite::create("feats_unlock.png");
        }
        else
            sprite = CCSprite::create("feats_lock.png");
        gap = m_leftDownSize.width - sprite->getTextureRect().size.width;
        sprite->setPosition(ccp(gap / 2,
                                gap + sprite->getTextureRect().size.height * i));
        scrollView->addChild(sprite, 0, i);
        height += sprite->getTextureRect().size.height;
    }
    scrollView->setContentSize(CCSizeMake(m_leftDownSize.width, height + gap * 2));
    scrollView->setContentOffset(CCPoint(0, - scrollView->getContentSize().height + m_leftDownSize.height));
///////////////////////////////////////////////////////////////////////////////////////////////
    
    m_itemNode = ItemNode::create();
    this->addChild(m_itemNode);
    CCPoint itemsPoint = ccp(skillFormBGSprite->getPositionX() - m_rightUpSize.width + 1,
                             skillFormBGSprite->getPositionY());
    m_itemNode->setPosition(itemsPoint);
    m_itemNode->setScrollSize(skillFormBGSprite->getTextureRect().size);
    
    
    return true;
}


bool TamerScene::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
    m_currentDragSprite = m_itemNode->touchStartWithPoint(pTouch->getLocation());
    return true;
}
void TamerScene::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
    
}
void TamerScene::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
    
}